uniform sampler2DRect inputTexture;
uniform sampler2DRect potentialTexture;

void main(void){
    mat3 I;
    mat3 pots;
    float potential;
    float outs, inib;
    float temp;
    vec3 sample;
    vec2 uv = gl_TexCoord[0].st;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            sample = texture2DRect(inputTexture, ivec2(uv) + ivec2(i-1, j-1)).rgb;
            I[i][j] = dot(sample, vec3(0.299,0.587,0.114));
        }
    }
    
    outs = I[1][1]*8.0; 
    inib = I[0][0] + I[0][1] + I[0][2] + I[1][0] + I[1][2] + I[2][0] + I[2][1] + I[2][2];

    outs = outs - inib;
    
    temp = outs;
    
    if(temp > 0.1) temp = 1.0;
     
    potential = texture2DRect(potentialTexture, ivec2(uv)).r;
    
    potential += outs;
    
    if(potential > 0.05)
    {
        potential = 0.0;
        outs = 1.0;
    }
    else
    {
        outs = 0.0;
    }
    
    
    
    float cnvx = I[0][0] + 2.0*I[0][1] + I[0][2] - I[2][0] - 2.0*I[2][1] - I[2][2];
    cnvx = cnvx*cnvx;

    float cnvy = I[0][0] - I[0][2] + 2.0*I[1][0] - 2.0*I[1][2] + I[2][0] - I[2][2];
    cnvy = cnvy*cnvy;

    vec3 c = vec3(0.5 * sqrt(cnvx*cnvx + cnvy*cnvy));    
    
    
    
    
     
    gl_FragData[0] = vec4(vec3(temp), 1.0);
    gl_FragData[1] = vec4(potential,0.0,0.0, 1.0);    
}
